package config

import (
	"github.com/toolkits/pkg/logger"
	"os"
	"strings"
)

// 参数设置示例：-con=dev-conf.yaml

var ArgMap map[string]string

func init() {
	// 获取启动参数
	ArgMap = getArgMap()
}

func getArgMap() (argMap map[string]string) {
	argMap = make(map[string]string)
	args := os.Args[1:]
	argMap["init"] = "ok"
	if len(args) == 0 {
		return argMap
	}
	for _, arg := range args {
		if !strings.HasPrefix(arg, "-") {
			continue
		}
		argList := strings.Split(arg, "=")
		if len(argList) == 2 {
			key := strings.TrimLeft(argList[0], "-")
			argMap[key] = argList[1]
		}
	}
	return argMap
}

func getConfigFileName() (configFileName string) {
	if v, ok := ArgMap["conf"]; ok {
		configFileName = v
	} else {
		configFileName = "conf.yaml"
	}
	logger.Info("使用配置文件：", configFileName)
	return configFileName
}
